package com.py.credit.controller;


import com.alibaba.fastjson.JSONObject;
import com.py.credit.common.ServiceException;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.service.DataCollectionService;
import com.py.credit.service.ErrorDataService;
import com.py.credit.service.RemoteBaseService;
import com.py.credit.service.XydDataCollectionService;
import com.py.framework.core.helper.HttpServletHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/xydCollection")
public class XydDataCollectionController {

    @Autowired
    private XydDataCollectionService xydDataCollectionService;

    @Autowired
    private RemoteBaseService remoteBaseService;
    
	@Autowired
	private ErrorDataService errorDataService;

    /**
     *
     * @api {get} /batchLog/manualCollection.json 手工采集
     * @apiName manualCollection
     * @apiVersion 0.0.1
     * @apiGroup 数据采集
     * @apiParam {String} data 数据json
     * @apiParam {Integer} tableId 数据资源表ID
     * @apiParam {Integer} rootId 库ID
     * @apiParamExample {json} 请求参数示例 {}
     * @apiSuccess {String} code 状态码:1 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {String} data 结果集
     * @apiSuccessExample {json} 成功返回结果 {}
     */
    @ResponseBody
    @RequestMapping(value = "/manualCollection.json")
    public RestServiceResult<JSONObject> manualCollection(HttpServletRequest request,
                                                          @RequestParam(value = "data", defaultValue = "", required = true) String data,
                                                          @RequestParam(value = "tableId", defaultValue = "", required = true) Integer tableId) {
        RestServiceResult<JSONObject> result = new RestServiceResult<>();
        // 获取当前用户信息
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("BatchLogController#manualCollection", "未获取到当前用户信息！");
                return result;
            }
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
        try {
        	//获取当前使用的版本ID
			Integer contentId = errorDataService.getcontentId();
            xydDataCollectionService.manualCollection(data, tableId, userInfo, contentId);
            return result;
        } catch (ServiceException e) {
            // 捕获抛出的异常
            String error = e.getMsg();
            JSONObject jb = new JSONObject();
            jb.put("error", error);
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(error);
            result.setData(jb);
            return result;
        } catch (Exception e) {
            ExceptionLogger.error("BatchLogController#manualCollection", "数据上报执行异常！ERROR:" + e.getMessage(), e);
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("上报数据执行异常！");
            return result;
        }
    }


}
